﻿using System.Collections.Generic;
using System.Linq;
using Test30.Core;

namespace Test30.Engine
{
    public static class EnumerableExtensions
    {
        public static T GetAt<T>(this IEnumerable<T> collection, Vec pos) where T : IPosition
        {
            foreach (T item in collection)
            {
                if (item.Position.Equals(pos))
                {
                    return item;
                }
            }

            // no item here
            return default(T);
        }

        public static IList<T> GetAllAt<T>(this IEnumerable<T> collection, Vec pos) where T : IPosition
        {
            List<T> items = new List<T>();

            foreach (T item in collection)
            {
                if (item.Position.Equals(pos))
                {
                    items.Add(item);
                }
            }

            return items;
        }

        public static int CountAt<T>(this IEnumerable<T> collection, Vec pos) where T : IPosition
        {
            return collection.Count(item => item.Position == pos);
        }
    }
}
